import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.util.PublicCloneable;

public class LegendXYItemLabelGenerator extends StandardXYItemLabelGenerator
		implements XYItemLabelGenerator, Cloneable, PublicCloneable,
		Serializable {
	private static final long serialVersionUID = -5877194728348062394L;
	private LegendItemCollection legendItems;
	private double offsetForLegend = 0;

	public LegendXYItemLabelGenerator(LegendItemCollection legendItems) {
		super();
		this.legendItems = legendItems;
	}

	public LegendXYItemLabelGenerator(LegendItemCollection legendItems,
			double offset) {
		super();
		offsetForLegend = offset;
		this.legendItems = legendItems;
	}

	
	public String generateLabel2(XYDataset dataset, int series, int item) {
		DecimalFormat formatter = (DecimalFormat) NumberFormat
				.getInstance(Locale.US);

		double valueToSend = ((XYSeriesCollection) dataset).getSeries(series)
				.getY(item).doubleValue()
				- offsetForLegend == 0 ? ((XYSeriesCollection) dataset)
				.getSeries(series).getY(item).doubleValue()
				: ((XYSeriesCollection) dataset).getSeries(series).getY(item)
						.doubleValue()
						- offsetForLegend;
		if (valueToSend < 0)
			return "";
		else
			return formatter.format(valueToSend);
		// String.valueOf(((XYSeriesCollection)dataset).getSeries(series).getY(item));

	}

	@Override
	public String generateLabel(XYDataset dataset, int series, int item) {
		DecimalFormat formatter = (DecimalFormat) NumberFormat
				.getInstance(Locale.US);

		if (series == 1) {
			if (((XYSeriesCollection) dataset).getSeries(series).getY(item)
					.doubleValue() == ((XYSeriesCollection) dataset)
					.getSeries(0).getY(item).doubleValue()) {
				return "";
			} else
				return formatter.format(((XYSeriesCollection) dataset)
						.getSeries(series).getY(item).doubleValue());
			// String.valueOf(((XYSeriesCollection)dataset).getSeries(series).getY(item));

		}
		if (series == 2) {
			if (((XYSeriesCollection) dataset).getSeries(series).getY(item)
					.doubleValue() == ((XYSeriesCollection) dataset)
					.getSeries(0).getY(item).doubleValue()) {
				return "";
			} else
				return formatter.format(((XYSeriesCollection) dataset)
						.getSeries(series).getY(item).doubleValue());

		}

		return formatter.format(((XYSeriesCollection) dataset)
				.getSeries(series).getY(item).doubleValue());

	}
}
